package com.spark.colliery

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.support.v4.view.PagerAdapter
import android.support.v4.view.ViewPager
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.view.ViewGroup
import android.widget.EdgeEffect
import android.widget.ImageView
import android.widget.LinearLayout
import com.blankj.utilcode.util.SPUtils
import com.spark.common.Const
import com.spark.colliery.module.mine.activity.LoginActivity
import kotlinx.android.synthetic.main.act_guide.*

/**
 * 引导页
 */
class GuideActivity : AppCompatActivity() {
    private var leftEdge: EdgeEffect? = null
    private var rightEdge: EdgeEffect? = null

    companion object {
        const val reqCode = 0x31
        fun actionStartBack(host: Activity) {
            host.startActivityForResult(Intent(host, GuideActivity::class.java), reqCode)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.act_guide)
        hack()
        val guidePagerAdapter = GuidePagerAdapter()
        vp_guide.adapter = guidePagerAdapter
        vp_guide.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}

            override fun onPageSelected(position: Int) {}

            override fun onPageScrollStateChanged(state: Int) {
                if (rightEdge != null && !rightEdge!!.isFinished) {
                    vp_guide.clearAnimation()
                    gotoNext()
                }
            }
        })
        findViewById<View>(R.id.btn_skip).setOnClickListener { v -> gotoNext() }

    }

    private fun gotoNext() {
        SPUtils.getInstance().put("init_install", false)
        if (!SPUtils.getInstance(Const.sp_user).getBoolean(Const.isOnline))
            LoginActivity.actionStart(this)
        else
            MainActivity.actionStart(this)
        finish()
    }


    /**
     * 获取到Edge
     */
    private fun hack() {
        try {
            val mLeftEdge = vp_guide.javaClass.getDeclaredField("mLeftEdge")
            val mRightEdge = vp_guide.javaClass.getDeclaredField("mRightEdge")
            if (mLeftEdge != null && mRightEdge != null) {
                mLeftEdge.isAccessible = true
                mRightEdge.isAccessible = true
                leftEdge = mLeftEdge.get(vp_guide) as EdgeEffect
                rightEdge = mRightEdge.get(vp_guide) as EdgeEffect
            }
        } catch (e: NoSuchFieldException) {
            e.printStackTrace()
        } catch (e: IllegalAccessException) {
            e.printStackTrace()
        }

    }

    /**
     * <><><><><><>adapter>>>>>>
     */
    internal inner class GuidePagerAdapter : PagerAdapter() {
        private val guideRes = intArrayOf(R.drawable.guide1, R.drawable.guide2)

        override fun getCount(): Int {
            return guideRes.size
        }

        override fun isViewFromObject(view: View, `object`: Any): Boolean {
            return view === `object`
        }

        override fun instantiateItem(container: ViewGroup, position: Int): Any {
            val imageView = ImageView(container.context)
            imageView.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)
            imageView.scaleType = ImageView.ScaleType.CENTER_CROP
            imageView.setImageResource(guideRes[position])
            container.addView(imageView)
            return imageView
        }

        override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
            container.removeView(`object` as View)
        }
    }
}
